﻿(function () {

    window.addFav = function (sURL, sTitle) {
        if (!sTitle) sTitle = document.title;
        if (!sURL) sURL = window.location.href;
        try {
            window.external.addFavorite(sURL, sTitle);
        } catch (e) {
            try {
                window.sidebar.addPanel(sTitle, sURL, "");
            } catch (e) {
                alert("加入收藏失败，请使用Ctrl+D进行添加,或手动在浏览器里进行设置.");
            }
        }
        return false;
    };

    window.setFont = function (elementId, fontSize, lineHeight) {
        var ele = document.getElementById(elementId);
        if (!ele) return;
        ele.style.fontSize = fontSize;
        ele.style.lineHeight = lineHeight;
        var childs = ele.childNodes;
        for (var i = 0; i < childs.length; i++) {
            if (childs[i].nodeType == 1) {
                childs[i].style.fontSize = fontSize;
                childs[i].style.lineHeight = lineHeight;
            }
        }
    };

    window.printArea = function (elementId) {
        var ele = document.getElementById(elementId);
        if (!ele) return;

        var hrefs = location.href.split('/');
        var eleHtml = ('<style type="text/css">.print-hide{display: none;}</style>' + ele.innerHTML).replace(new RegExp('(' + ' src="/' + ')', 'g'), ' src="' + hrefs[0] + '//' + hrefs[2] + '/');

        var newWindow = window.open("打印窗口", "_blank");
        newWindow.document.write(eleHtml);
        newWindow.document.close();
        newWindow.print();
        //newWindow.close();
    };

    function _standalone(element, document, navigator, standalone) {
        var isStandalone = (standalone in navigator) && navigator[standalone];
        if (!isStandalone) return;
        var links = element.getElementsByTagName('a');
        for (var i = 0; i < links.length; i++) {
            links[i].addEventListener('click', function (event) {
                var link = this;
                var href = link.getAttribute('href');
                if (href.indexOf('#') == 0 || href.indexOf(':') != -1) return;
                event.preventDefault();
                window.location = href;
            });
        }
    };
    window.Standalone = function (element) {
        if (!element) element = document;
        _standalone(element, document, window.navigator, 'standalone');
    };
    Standalone();

    var smartImageLastSize = '';
    var smartImageTimeout;
    window.SmartImage = function () {
        var computedStyle = window.getComputedStyle(document.body, ':after');
        if (!computedStyle) return;
        var propertyValue = computedStyle.getPropertyValue('content');
        if (!propertyValue) return;
        var size = propertyValue;
        if (smartImageLastSize == size)
            return;
        smartImageLastSize = size;
        var imgs = document.getElementsByTagName('img');
        var nimgs = [];
        for (var j = 0; j < imgs.length; j++) {
            var img = imgs[j];
            var src = img.getAttribute('data-src-' + size);
            if (!src) continue;
            nimgs[j] = new Image();
            nimgs[j].src = src;
            nimgs[j].addEventListener('load', (function (img, src) {
                img.src = src;
            })(img, src));
        }
    };
    window.onresize = function () {
        if (smartImageTimeout) { clearTimeout(smartImageTimeout) };
        smartImageTimeout = setTimeout(SmartImage, 20);
    };
    SmartImage();
})();  /*fa fa-star

(function ($) {
    $('#header_icons .a-icon-star').click(function () {
        addFav();
    });
    $('#favbtn').click(addFav);
    Standalone();
})(jQuery);*/